package com.example.demo.controller;


import com.example.demo.entity.DBConnection;
import com.example.demo.entity.ResponseInfo;
import com.example.demo.service.DBConnectionService;
import com.example.demo.utils.PageInfoNew;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Validated
@RequestMapping("/dbConnection")
@io.swagger.annotations.Api(value = "/dbConnection", tags = "数据库连接接口")
public class DbConnectionController {

    @Resource
    DBConnectionService dbConnectionService;

//
//    @PostMapping("/test")
//    public ResponseInfo<List<Map<String, String>>> test() {
//        String sql = "Select * from sales_order where genre = 1";
//        DBConnection dbConnection = new DBConnection();
//        dbConnection.setDbConnectionUrl("jdbc:mysql://192.168.17.236:30512/tenant_commercial_concrete?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&characterEncoding=UTF8");
//        dbConnection.setDbType(1);
//        dbConnection.setDbConnectionUser("root");
//        dbConnection.setDbConnectionPassword("laihui");
//        return ResponseInfo.successInfo(dbConnectionService.executeQuery(dbConnection, sql));
//    }

    @PostMapping("/save")
    @ApiOperation(value = "数据库连接添加接口")
    public ResponseInfo<Integer> save(@RequestBody DBConnection dbConnection) {
        return ResponseInfo.successInfo(dbConnectionService.save(dbConnection));
    }

    @PostMapping("/update")
    @ApiOperation(value = "数据库连接更新接口")
    public ResponseInfo<Integer> update(@RequestBody DBConnection dbConnection) {
        return ResponseInfo.successInfo(dbConnectionService.update(dbConnection));
    }

    @GetMapping("/list")
    @ApiOperation(value = "数据库连接列表接口")
    @ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "int")
    public ResponseInfo<List<DBConnection>> list(@RequestParam int projectId) {
        return ResponseInfo.successInfo(dbConnectionService.list(projectId));
    }

    @GetMapping("/page")
    @ApiOperation(value = "数据库连接分页接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页长度", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "Integer")
    }
    )
    public ResponseInfo<PageInfoNew<DBConnection>> page(@RequestParam Integer pageNum,
                                                        @RequestParam Integer pageSize,
                                                        @RequestParam Integer projectId) {
        return ResponseInfo.successInfo(dbConnectionService.page(pageNum, pageSize, projectId));
    }

    /**
     * @param id 传入apiId
     * @return 获取返回结果
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("数据库连接删除接口")
    public ResponseInfo<String> delete(@PathVariable(value = "id") int id) {
        dbConnectionService.delete(id);
        return ResponseInfo.successInfo("");
    }

    @GetMapping("/detail")
    @ApiOperation(value = "数据库连接详情接口")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int")
    public ResponseInfo<DBConnection> detail(@RequestParam int id) {
        return ResponseInfo.successInfo(dbConnectionService.findById(id));
    }


}
